﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DHM.IService;
using DHM.Model;
using DHM.Model.DTO;
using DHM.WXMiniProgram.Utility.APIResult;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;

namespace DHM.WXMiniProgram.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class UserLikesController : ControllerBase
    {
        private readonly IUserLikesService _userLikesService;
        public UserLikesController(IUserLikesService userLikesService)
        {
            _userLikesService = userLikesService;
        }
        /// <summary>
        /// 用户点赞
        /// </summary>
        /// <param name="userLikes"></param>
        /// <returns></returns>
        [HttpPost("AddLike")]
        public async Task<APIResult> AddLike([FromServices] IMapper iMapper,[FromForm]UserLikesDtoInput userLikesDto)
        {

            var userLikes = iMapper.Map<UserLikes>(userLikesDto);

            var openid = userLikes.OpenID;
            var module = userLikes.CommentModule;
            var like = await _userLikesService.FindAsync(x => x.OpenID.Equals(userLikes.OpenID));

            if (like != null)
            {
                var result = JudgeUserLiked(userLikes, like, true);

                await _userLikesService.UpdateAsync(like);

                return result;
            }
            else
            {
                // var result= JudegUserLiked()
                var us = JudgeUserLiked(ref userLikes);

                await _userLikesService.AddAsync(us);

                var result = APIResultHelper.Success(string.Format("新用户{0}点赞成功", userLikes.CommentModule));

                return result;
            }
        }
        #region 获取用户点赞结果
        /// <summary>
        /// 判断数据库中是否有该用户，并且判断是否该用户对特定模块点过赞
        /// </summary>
        /// <param name="userLikes"></param>
        /// <param name="like"></param>
        /// <returns></returns>
        private UserLikes JudgeUserLiked(ref UserLikes userLikes)
        {
            UserLikes us = null;
            switch (userLikes.CommentModule)
            {
                case CommentModule.SZYZ:
                    userLikes.SZYZ_Like = true;
                    break;
                case CommentModule.SWF:
                    userLikes.SWF_Like = true;
                    break;
                case CommentModule.MHC:
                    userLikes.MHC_Like = true;
                    break;
                case CommentModule.SDMS:
                    userLikes.SDMS_Like = true;
                    break;
                case CommentModule.AR:
                    userLikes.AR_Like = true;
                    break;
                default:
                    break;
            }
            us = userLikes;
            return us;
        }
        private APIResult JudgeUserLiked(UserLikes userLikes, UserLikes like, bool isLiked)
        {
            APIResult result = null;
            switch (userLikes.CommentModule)
            {

                case CommentModule.SZYZ:
                    result = IsLike(like.SZYZ_Like, CommentModule.SZYZ);
                    like.SZYZ_Like = isLiked;
                    break;
                case CommentModule.SWF:
                    result = IsLike(like.SWF_Like, CommentModule.SWF);
                    like.SWF_Like = isLiked;
                    break;
                case CommentModule.MHC:
                    result = IsLike(like.MHC_Like, CommentModule.MHC);
                    like.MHC_Like = isLiked;
                    break;
                case CommentModule.SDMS:
                    result = IsLike(like.SDMS_Like, CommentModule.SDMS);
                    like.SDMS_Like = isLiked;
                    break;
                case CommentModule.AR:
                    result = IsLike(like.AR_Like, CommentModule.AR);
                    like.AR_Like = isLiked;
                    break;
                default:
                    break;
            }
            return result;
        }
        private APIResult IsLike(bool isLike, CommentModule commentModule)
        {
            if (isLike)
            {
                return APIResultHelper.Warning(string.Format("{0}已经点过赞了，无需重复", commentModule.ToString()));
            }
            else
            {
                return APIResultHelper.Success(string.Format("{0}点赞成功", commentModule.ToString()));
            }
        }
        #endregion
    }
}
